package com.sniff.atp.modules.test.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ckbs.global.common.utils.DingTalkNoticeUtils;
import com.sniff.atp.modules.test.mapper.DemandReportMapper;
import com.sniff.atp.modules.test.mapper.beework.ProjectMapper;
import com.sniff.atp.modules.test.model.BugRegister;
import com.sniff.atp.modules.test.model.DemandReport;
import com.sniff.atp.modules.test.model.PublishPlan;
import com.sniff.atp.modules.test.model.beework.Project;
import com.sniff.atp.modules.test.service.BugRegisterService;
import com.sniff.atp.modules.test.service.DemandReportService;
import com.sniff.atp.provider.DataSource;
import com.sniff.atp.provider.DataSourceType;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 需求登记 服务实现类
 * </p>
 *
 * @author macro
 * @since 2023-09-07
 */
@Service
public class DemandReportServiceImpl extends ServiceImpl<DemandReportMapper, DemandReport> implements DemandReportService {

    @Autowired
    private BugRegisterService registerService;

    @Autowired
    private DemandReportMapper demandReportMapper;

    @Autowired
    private ProjectMapper projectMapper;


    @Override
    public boolean create(DemandReport demandReport) {
        demandReport.setCreateTime(new Date());
        demandReport.setUpdateTime(new Date());

        //通知测试组群，现在弃用
//        String robotToken = "cfc90ef472d9b9cdf4ab1454f87178fba59f9b55aab462d63f920e6c43c9545a";
//        String robotKey = "SEC13d4dbaf148f723b89dfe1b71ac96d2dda76a9201e22c6d2c62808f40d124523";


//                需求对接信息同步群2
        String robotTokenDevelop = "7fdcab5f04ee8661a6b60399ce224ab649e36bb944787fe0341f978ac97722df";
        String robotKeyDevelop = "SEC7f1ecdf821df96c2d6bb970d73f58d6286849279666b6e6c24f0a534ef159ab0";

        // 定义日期格式
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
//        String robotToken = "c6c2c7c6c98c2fb5d234319afa9e0dad659152a8518ac739191811ce1bf8a67b";
//        String robotKey = "SECdf56e788259528d23a4b225863526a0bc426c178251313f1e4d5bdc5e9b696d6";
//        测试
        String demandTest = demandReport.getDemandTest();
//        后端开发
        String demandBackDevelop = demandReport.getDemandBackDevelop();
//        前端
        String demandFrontDevelop = demandReport.getDemandFrontDevelop();
//        数据
        String dataDevelop = demandReport.getDataDevelop();



        Date reviewDate = demandReport.getDemandReviewDate();
        Date submitApplicationDate = demandReport.getSubmitApplicationDate();
        Date publishDate = demandReport.getPublishDate();
        String addContent = "";

        if(demandTest !=null){
            addContent = addContent.concat("测试人员：").concat(demandTest).concat(";\n");
        }

        if(demandBackDevelop !=null){
            addContent = addContent.concat("后端开发：").concat(demandBackDevelop).concat(";\n");
        }

        if(demandFrontDevelop !=null){
            addContent = addContent.concat("前端开发：").concat(demandFrontDevelop).concat(";\n");
        }

        if(dataDevelop !=null){
            addContent = addContent.concat("数据开发：").concat(dataDevelop).concat(";\n");
        }

        if( reviewDate != null ){
            addContent = addContent.concat("需求评审：").concat(outputFormat.format(reviewDate)).concat(";\n");
        }
        if( submitApplicationDate != null ){
            addContent = addContent.concat("提测时间：").concat(outputFormat.format(submitApplicationDate)).concat(";\n");
        }
        if( publishDate != null ){
            addContent = addContent.concat("发布时间：").concat(outputFormat.format(publishDate)).concat(";\n");
        }


        List<String> mobileList = new ArrayList<>();
        String content = ("需求名称：【").concat(demandReport.getDemandType()).concat ("】 ").concat(demandReport.getDemandName()).concat(";\n")
                .concat (addContent);

        //测试群
//        DingTalkNoticeUtils.sendDingMsgAll(robotKey,robotToken,mobileList,content);

//        开发群
        DingTalkNoticeUtils.sendDingMsgAll(robotKeyDevelop,robotTokenDevelop,mobileList,content);

        return save(demandReport);
    }

    @Override
    public boolean update(Long id, DemandReport demandReport) {
        demandReport.setDemandId(id);
        demandReport.setUpdateTime(new Date());

        //是bug的更新bug状态
        try {
            Long bigId = demandReport.getBugId();
            String demandStatus = demandReport.getDemandStatus();
            if(bigId != null && ("已上线".equals(demandStatus) || "已发布".equals(demandStatus))){
                BugRegister bugRegister = new BugRegister();
                bugRegister.setBugId(bigId);
                bugRegister.setBugStatus("已解决");
                bugRegister.setUpdateTime(new Date());
                registerService.update(bugRegister.getBugId(), bugRegister);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return updateById(demandReport);
    }

    @Override
    public Page<DemandReport> list(DemandReport demandReport, Integer pageSize, Integer pageNum) {
        Page<DemandReport> page = new Page<>(pageNum,pageSize);
        QueryWrapper<DemandReport> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .like(StringUtil.isNotBlank(demandReport.getDemandName()), DemandReport::getDemandName,demandReport.getDemandName())
                .like(StringUtil.isNotBlank(demandReport.getDemandTest()), DemandReport::getDemandTest,demandReport.getDemandTest())
                .like(StringUtil.isNotBlank(demandReport.getDemandBackDevelop()), DemandReport::getDemandBackDevelop,demandReport.getDemandBackDevelop())
                .like(StringUtil.isNotBlank(demandReport.getDemandFrontDevelop()), DemandReport::getDemandFrontDevelop,demandReport.getDemandFrontDevelop())
                .like(StringUtil.isNotBlank(demandReport.getDataDevelop()), DemandReport::getDataDevelop,demandReport.getDataDevelop())
                .eq(StringUtil.isNotBlank(demandReport.getDemandType()),DemandReport::getDemandType,demandReport.getDemandType())
                .eq(StringUtil.isNotBlank(demandReport.getDemandStatus()),DemandReport::getDemandStatus,demandReport.getDemandStatus())
                .ge(demandReport.getStartTime() != null,DemandReport::getPublishDate,demandReport.getStartTime())
                .le(demandReport.getEndTime() != null,DemandReport::getPublishDate,demandReport.getEndTime())
                .ge(demandReport.getUpdateStartTime() != null,DemandReport::getUpdateTime,demandReport.getUpdateStartTime())
                .le(demandReport.getUpdateEndTime() != null,DemandReport::getUpdateTime,demandReport.getUpdateEndTime())
                .orderByDesc(DemandReport::getDemandStatus)
                .orderByDesc(DemandReport::getPublishDate);
        return page(page, wrapper);
    }

    //获取当天需要发布的内容
    @Override
    public List<DemandReport> qryDemandReportByPublishDate(DemandReport demandReport) {

        QueryWrapper<DemandReport> wrapper = new QueryWrapper<>();
        //查询当天还在开发中的数据
        wrapper.lambda()
                .eq(DemandReport::getPublishDate,demandReport.getPublishDate())
                .in(DemandReport::getDemandStatus,"开发中")
                .orderByDesc(DemandReport::getCreateTime);
//                .eq(StringUtil.isNotBlank(publishPlan.getTestCheckStatus()),PublishPlan::getTestCheckStatus,publishPlan.getTestCheckStatus())
        List<DemandReport> demandReportList = demandReportMapper.selectList(wrapper);
        return demandReportList;
    }

    /**
     * 获取当天需要发布的内容
     */
    @Override
    public List<DemandReport> qryDemandReportBySubmitDate(DemandReport demandReport) {

        QueryWrapper<DemandReport> wrapper = new QueryWrapper<>();
        //查询T-1 日期的，提测还在开发中的数据
        wrapper.lambda()
                .le(DemandReport::getSubmitApplicationDate,demandReport.getSubmitApplicationDate())
                .in(DemandReport::getDemandStatus,"开发中")
                .orderByDesc(DemandReport::getCreateTime);
//                .eq(StringUtil.isNotBlank(publishPlan.getTestCheckStatus()),PublishPlan::getTestCheckStatus,publishPlan.getTestCheckStatus())
        List<DemandReport> demandReportList = demandReportMapper.selectList(wrapper);
        return demandReportList;
    }

    //批量更新需求状态
    @Override
    public int batchUpdateDemandByCode(List<DemandReport> list){
        return demandReportMapper.batchUpdateDemandByCode(list);
    }

    //按需求id查询需求信息
    @Override
    public List<DemandReport> qryDemandInfoByIdList(List<Long> demandIdList){
        QueryWrapper<DemandReport> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(DemandReport::getDemandId,demandIdList);
        return demandReportMapper.selectList(wrapper);
    }

    //按业务线查询beework项目
    @Override
    @DataSource(DataSourceType.BEEWORK)
    public List<Project> qryBeeworkProduct(DemandReport demandReport) {
          // '项目状态 1-方案未开始 2-方案输出中 3-待需求内审 4-需求内审完成 5-需求评审完成 6-技术评审完成 7-开发中 8-测试中 9-需求已上线 10-需求已取消 11-需求暂停 12-需求已归档',
        List<Integer> projectStatus = Arrays.asList(9, 10);
        QueryWrapper<Project> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .notIn(Project::getProjectStatus, projectStatus)
                .eq(Project::getBusinessLine, demandReport.getDemandLine());
        return projectMapper.selectList(wrapper);
    }


}

